import { DataTypes, Model, Optional } from 'sequelize';
import sequelize from '../utils/database';

// 定义Category属性接口
export interface CategoryAttributes {
  id: number;
  name: string;
  parentId: number;
  level: number;
  sortOrder: number;
  icon?: string;
  image?: string;
  description?: string;
  isShow: number;
  createdAt?: Date;
  updatedAt?: Date;
}

// 定义创建Category时的可选属性
interface CategoryCreationAttributes extends Optional<CategoryAttributes, 'id' | 'createdAt' | 'updatedAt'> {}

// 定义Category模型类
class Category extends Model<CategoryAttributes, CategoryCreationAttributes> implements CategoryAttributes {
  public id!: number;
  public name!: string;
  public parentId!: number;
  public level!: number;
  public sortOrder!: number;
  public icon?: string;
  public image?: string;
  public description?: string;
  public isShow!: number;
  public readonly createdAt!: Date;
  public readonly updatedAt!: Date;

  // 实例方法：检查是否为顶级分类
  public isTopLevel(): boolean {
    return this.parentId === 0;
  }

  // 实例方法：获取显示状态文本
  public getShowStatusText(): string {
    return this.isShow === 1 ? '显示' : '隐藏';
  }
}

// 初始化Category模型
Category.init(
  {
    id: {
      type: DataTypes.INTEGER,
      autoIncrement: true,
      primaryKey: true,
      comment: '分类ID'
    },
    name: {
      type: DataTypes.STRING(100),
      allowNull: false,
      comment: '分类名称',
      validate: {
        notEmpty: {
          msg: '分类名称不能为空'
        },
        len: {
          args: [1, 100],
          msg: '分类名称长度必须在1-100个字符之间'
        }
      }
    },
    parentId: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: '父分类ID，0为顶级分类',
      field: 'parent_id'
    },
    level: {
      type: DataTypes.TINYINT,
      allowNull: false,
      defaultValue: 1,
      comment: '分类级别：1-一级，2-二级，3-三级',
      validate: {
        min: {
          args: [1],
          msg: '分类级别最小为1'
        },
        max: {
          args: [3],
          msg: '分类级别最大为3'
        }
      }
    },
    sortOrder: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: '排序',
      field: 'sort_order'
    },
    icon: {
      type: DataTypes.STRING(255),
      allowNull: true,
      comment: '分类图标'
    },
    image: {
      type: DataTypes.STRING(255),
      allowNull: true,
      comment: '分类图片'
    },
    description: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: '分类描述'
    },
    isShow: {
      type: DataTypes.TINYINT,
      allowNull: false,
      defaultValue: 1,
      comment: '是否显示：0-隐藏，1-显示',
      field: 'is_show',
      validate: {
        isIn: {
          args: [[0, 1]],
          msg: '显示状态只能是0或1'
        }
      }
    },
    createdAt: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
      comment: '创建时间',
      field: 'created_at'
    },
    updatedAt: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
      comment: '更新时间',
      field: 'updated_at'
    }
  },
  {
    sequelize,
    tableName: 'categories',
    timestamps: true,
    createdAt: 'createdAt',
    updatedAt: 'updatedAt',
    indexes: [
      {
        fields: ['parent_id']
      },
      {
        fields: ['level']
      },
      {
        fields: ['is_show']
      }
    ]
  }
);

export default Category;